package com.kcloud.zkem.web;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.kcloud.zkem.config.ZKEMProperties;
import com.kcloud.zkem.domain.LogData;
import com.kcloud.zkem.utils.ZkemSDKUtils;

import io.swagger.annotations.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 考勤对外接口
 * @author guor
 */
@RestController
@RequestMapping("/zkem")
public class ZKEMController {
	 Logger logger = LoggerFactory.getLogger(ZKEMController.class);
    @Autowired
    ZKEMProperties zkemProperties;
    @ApiOperation(value ="获取考勤数据",tags = "中控考勤机服务",response = JSONObject.class,notes = "获取一天的考勤数据，可以指定考勤机中的用户编号，指定获取考勤的日期")
    @ApiOperationSupport(
            author = "GuoR",
            responses = @DynamicResponseParameters( properties = {
                    @DynamicParameter(value = "状态码 0为成功，1为失败",name = "code",dataTypeClass = Integer.class),
                    @DynamicParameter(value = "数据集",name = "data"),
                    @DynamicParameter(value = "失败信息",name = "message")
            })
    )
    @GetMapping("/getUserLogInfo")
    public JSONObject getUserLogInfo(
            @ApiParam(value = "考勤机中用户编号",name = "userNumber" ) @RequestParam(required = false) String userNumber,
            @ApiParam(value = "获取考勤的日期，如果为空为当前天 。日期格式为：yyyy-MM-dd",name = "searchDate" ,example = "2019-11-19") @RequestParam(required = false)  String searchDate){
        JSONObject json = JSONUtil.createObj();
        try {
            ZkemSDKUtils utils = new ZkemSDKUtils();
            boolean connect = isConnect(utils);
            if (connect) {
                boolean isRead = utils.readGeneralLogData();
                if(isRead){
                    List<LogData> userLogInfo = utils.getUserOneDayInfo(userNumber,searchDate);
                    json.put("success",0);
                    json.put("data",userLogInfo);
                }else{
                    json.put("success",1);
                    json.put("message","读取考勤记录到pc缓存失败");
                }
            }else{
                json.put("success",1);
                json.put("message",zkemProperties.getHost()+":"+zkemProperties.getPort()+" 连接失败");
            }
            disConnect(utils);
        }catch (Error e){
            json.put("success",1);
            json.put("message",e.getMessage());
        }
        return json;
    }
    @ApiOperation(value ="获取考勤机状态",tags = "中控考勤机服务",response = JSONObject.class,notes = "" +
            "1 管理员数量\n" +
            "2 注册用户数量\n" +
            "3 机器内指纹模板数量\n" +
            "4 密码数量\n" +
            "5 操作记录数\n" +
            "6 考勤记录数\n" +
            "7 指纹模板容量\n" +
            "8 用户容量\n" +
            "9 考勤记录容量\n" +
            "10 剩余指纹模板容量\n" +
            "11 剩余用户数容量\n" +
            "12 剩余考勤记录容量\n" +
            "21 人脸总数\n" +
            "22 人脸容量\n" +
            "其他状况返回 0")
    @ApiOperationSupport(
            author = "GuoR",
            responses = @DynamicResponseParameters( properties = {
                    @DynamicParameter(value = "状态码 0为成功，1为失败",name = "code",dataTypeClass = Integer.class),
                    @DynamicParameter(value = "数据集",name = "data"),
                    @DynamicParameter(value = "失败信息",name = "message")
            })
    )
    @GetMapping("/getDeviceStatus")
    public JSONObject getDeviceStatus(
            @ApiParam(value = "状态编码:",example = "12",allowableValues = "1,2,3,4,5,6,7,8,9,10,11,12,21,22,0",type = "int",required = true) @RequestParam(value = "statusCode") Integer statusCode
            ){
        JSONObject json = JSONUtil.createObj();
        try {
            ZkemSDKUtils utils = new ZkemSDKUtils();
            boolean connect = isConnect(utils);
            if (connect) {
                Integer deviceStatus = utils.getDeviceStatus(statusCode);
                json.put("success",0);
                json.put("data",deviceStatus);
            }else{
                json.put("success",1);
                json.put("message",zkemProperties.getHost()+":"+zkemProperties.getPort()+" 连接失败");
            }
            disConnect(utils);
        }catch (Error e){
            json.put("success",1);
            json.put("message",e.getMessage());
        }
        return json;
    }

    private boolean isConnect(ZkemSDKUtils utils) {
        boolean connect = utils.connect(zkemProperties.getHost(), zkemProperties.getPort());
        logger.info(zkemProperties.getHost()+":"+zkemProperties.getPort()+" 连接状态 ====》"+connect);
        return connect;
    }
    private void disConnect(ZkemSDKUtils utils){
        utils.disableDeviceWithTimeOut();
        utils.disconnect();
    }
}
